//
//  RecommendViewController.m
//  TravelToHelp
//
//  Created by lanou3g on 15/10/28.
//  Copyright © 2015年 冯贵宾. All rights reserved.
//

#import "RecommendViewController.h"
#import "FLSREcommendHeadView.h"
#import "RecommondCityViewController.h"
#import "ReHotCViewController.h"
#import "ReFunnySViewController.h"

@interface RecommendViewController ()<UIScrollViewDelegate>

// 头部导航栏
@property (nonatomic, strong) FLSREcommendHeadView *naviView;

@property (nonatomic, assign) CGPoint center;

// 大背景scrollView
@property (nonatomic, strong) UIScrollView *bgScrollView;

// 子视图控制器
@property (nonatomic, strong) ReHotCViewController *hotVC;

@property (nonatomic, strong) ReFunnySViewController *seasonVC;

@end

@implementation RecommendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"TestNaviBar2.jpg"] forBarMetrics:(UIBarMetricsDefault)];
    [self.navigationController.navigationBar setTranslucent:NO];
    [self addSubViews];
}

// 添加视图
- (void)addSubViews
{
    // 导航栏设置
    self.naviView = [[FLSREcommendHeadView alloc] initWithFrame:CGRectMake((self.view.width - 240) / 2, 20, 240, 28)];
    self.naviView.layer.cornerRadius = 14;
    self.naviView.layer.masksToBounds = YES;
    self.naviView.alpha = 0.5;
    self.naviView.layer.borderWidth = 0.5;
    self.naviView.layer.borderColor = [UIColor whiteColor].CGColor;
    self.center = self.naviView.motionView.center;
    [self.naviView.hotCity addTarget:self action:@selector(actionHotC:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.naviView.funnySeason addTarget:self action:@selector(actionFunnyS:) forControlEvents:(UIControlEventTouchUpInside)];
    self.navigationItem.titleView = self.naviView;
    
    // 导航栏左边城市切换Barbutton
//    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-chengshi"] style:(UIBarButtonItemStylePlain) target:self action:@selector(actionLeftBar:)];
//    self.navigationItem.leftBarButtonItem = leftBarButton;
    
    // 添加大背景scrollView
    _bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth , kScreenHeight - 49)];
    _bgScrollView.contentSize = CGSizeMake(kScreenWidth * 2, kScreenWidth - 96); // 滚动范围
    _bgScrollView.bounces = NO; // 不回弹
    _bgScrollView.pagingEnabled = YES; // 整屏滚动
    _bgScrollView.delegate = self;
    _bgScrollView.showsHorizontalScrollIndicator = NO; // 不显示水平滚动条
    [self.view addSubview:_bgScrollView];

    // 添加子控制器,并显示视图
    // 精选
    ReHotCViewController *hotVC = [[ReHotCViewController alloc] init];
    [self addChildViewController:hotVC];
    [self.bgScrollView addSubview:hotVC.view];
    ReFunnySViewController *seasonVC = [[ReFunnySViewController alloc] init];
    [self addChildViewController:seasonVC];
    seasonVC.view.frame = CGRectMake(self.view.width, 0, self.view.width, self.view.height);
    [self.bgScrollView addSubview:seasonVC.view];
    
   
}

#pragma mark -- 顶部导航栏的button实现
- (void)actionHotC:(UIButton *)button
{
    self.naviView.motionView.center = self.center;
    [self.naviView.funnySeason setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [self.naviView.hotCity setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
    _bgScrollView.contentOffset = CGPointMake(0, 0) ;
}

- (void)actionFunnyS:(UIButton *)button
{
    self.naviView.motionView.center = CGPointMake(self.center.x + 120, self.center.y);
    [self.naviView.hotCity setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [self.naviView.funnySeason setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
    _bgScrollView.contentOffset = CGPointMake(_bgScrollView.width * 1, 0) ;
}
// 城市切换
//- (void)actionLeftBar:(UIBarButtonItem *)barButton
//{
//    RecommondCityViewController *cityVC = [[RecommondCityViewController alloc] init];
//    cityVC.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:cityVC animated:YES];
//}

#pragma mark -- scrollView协议
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat dx = scrollView.contentOffset.x * 120 / kScreenWidth ;
    CGPoint tempCenter = self.center;;
    tempCenter.x = self.center.x + dx;
    self.naviView.motionView.center = tempCenter;
    if (scrollView.contentOffset.x > 0) {
        [self.naviView.funnySeason setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
        [self.naviView.hotCity setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    } else {
        [self.naviView.hotCity setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
        [self.naviView.funnySeason setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    }
    
}


@end
